﻿using System;
using System.Collections.Generic;
using System.Linq;

public class Student
{
    public string Name { get; set; }
    public int Age { get; set; }
}

public class Program
{
    public static void Main()
    {
        List<Student> students = new List<Student>
        {
            new Student { Name = "张三", Age = 20 },
            new Student { Name = "李四", Age = 18 },
            new Student { Name = "王五", Age = 22 }
        };
        //// 使用 Sort 方法按年龄排序
        //students.Sort((x, y) => x.Age.CompareTo(y.Age));
        //students.Sort((x, y) => -y.Age.CompareTo(x.Age));
        //students.Sort((x, y) => y.Age.CompareTo(x.Age));

        //// 输出排序后的学生名单
        //foreach (var student in students)
        //{
        //    Console.WriteLine($"{student.Name}, {student.Age}");
        //}


        // 使用 OrderBy 方法按年龄排序
        var sortedStudents = students.OrderBy(s => s.Age);

        // 输出排序后的学生名单
        foreach (var student in sortedStudents)
        {
            Console.WriteLine($"{student.Name}, {student.Age}");
        }

        //// 使用 OrderByDescending 方法按年龄降序排序
        //var sortedStudentsDesc = students.OrderByDescending(s => s.Age);

        //// 输出排序后的学生名单
        //foreach (var student in sortedStudentsDesc)
        //{
        //    Console.WriteLine($"{student.Name}, {student.Age}");
        //}
    }
}
